import { IsString, IsNumber, IsOptional, IsArray } from 'class-validator';
import { Transform, Type } from '@nestjs/class-transformer';

// 自定义转换函数，保留原始字符串值
const keepOriginalString = <T>(value: T) => {
  return value;
};

export class CreateSongDto {
  @IsString()
  title: string;

  @IsNumber()
  artistId: number;

  @IsOptional()
  @IsNumber()
  albumId?: number;

  @IsString()
  genre: string;

  @IsNumber()
  duration: number; // 以秒为单位

  @IsString()
  audioUrl: string;

  @IsOptional()
  @IsString()
  coverImageUrl?: string;

  @IsOptional()
  @IsString()
  lyrics?: string;

  @IsOptional()
  @IsArray()
  @Type(() => Number)
  categoryIds?: number[];
}

export class UpdateSongDto {
  @IsOptional()
  @IsString()
  title?: string;

  @IsOptional()
  @IsNumber()
  artistId?: number;

  @IsOptional()
  @IsNumber()
  albumId?: number;

  @IsOptional()
  @IsString()
  genre?: string;

  @IsOptional()
  @IsNumber()
  duration?: number;

  @IsOptional()
  @IsString()
  audioUrl?: string;

  @IsOptional()
  @IsString()
  coverImageUrl?: string;

  @IsOptional()
  @IsString()
  lyrics?: string;

  @IsOptional()
  @IsArray()
  @Type(() => Number)
  categoryIds?: number[];
}

export class SongResponseDto {
  id: number;
  title: string;
  artist: { id: number; name: string };
  album?: { id: number; title: string };
  genre: string;
  duration: number;
  audioUrl: string;
  coverImageUrl?: string;
  lyrics?: string;
  plays: number;
  likes: number;
  createdAt: Date;
  updatedAt: Date;
  uploadedBy?: { id: number; username: string };
  categories?: Array<{ id: number; name: string }>;
}

/**
 * 每日推荐歌曲查询参数DTO
 * 使用Transform装饰器保留原始字符串值，避免全局ValidationPipe的自动类型转换
 */
export class DailyRecommendationsQueryDto {
  @IsOptional()
  @Transform(keepOriginalString)
  limit?: string;
}

/**
 * 歌曲列表分页查询参数DTO
 */
export class SongsPaginationQueryDto {
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  page?: number = 1;

  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  limit?: number = 10;

  @IsOptional()
  @IsString()
  genre?: string;

  @IsOptional()
  @IsString()
  artist?: string;

  @IsOptional()
  @IsString()
  album?: string;

  @IsOptional()
  @IsString()
  searchTerm?: string;
}

/**
 * 分页响应DTO
 */
export class PaginationResponseDto<T> {
  data: T[];
  meta: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
    hasNextPage: boolean;
    hasPreviousPage: boolean;
  };
}

/**
 * 通过分类获取歌曲的查询参数DTO
 */
export class SongsByCategoryQueryDto {
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  categoryId?: number;

  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  page?: number = 1;

  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  limit?: number = 10;
}
